﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using TestStepsEditor.Actions;
using TestStepsEditor.Viewers.Data;

namespace TestStepsEditor.Forms
{
    public partial class FindTextForm : Form
    {
        public SharedData Data { get; set; }
        public List<DataGridViewCell> FoundText { get; set; }
        public FindTextForm()
        {
            InitializeComponent();
            FoundText = new List<DataGridViewCell>();
        }

        private void OKbutton_Click(object sender, EventArgs e)
        {
            if (Data.StepsCollection != null)
            {
                FoundText = Rows.Find(Data, findTextBox.Text);
                if (FoundText.Count > 0)
                {
                    var desiredCell = Data.GridView.Rows[FoundText.First().RowIndex];
                    Data.GridView.CurrentCell = desiredCell.Cells[FoundText.First().ColumnIndex];
                }
            }
        }

        private void nextButton_Click(object sender, EventArgs e)
        {
            if (Data.StepsCollection != null)
            {
                if (FoundText.Count == 0)
                {
                    FoundText = Rows.Find(Data, findTextBox.Text);
                    var desiredCell = Data.GridView.Rows[FoundText.First().RowIndex];
                    Data.GridView.CurrentCell = desiredCell.Cells[FoundText.First().ColumnIndex];
                    FoundText.Remove(FoundText.First());
                }
                else
                {
                    var desiredCell = Data.GridView.Rows[FoundText.First().RowIndex];
                    Data.GridView.CurrentCell = desiredCell.Cells[FoundText.First().ColumnIndex];
                    FoundText.Remove(FoundText.First());
                }
            }
        }
    }
}
